package com.uniknow.app.util;


import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.uniknow.app.AppContext;
import com.uniknow.app.AppManager;
import com.uniknow.app.R;

/**
 * 应用程序UI工具包：封装UI相关的一些操作
 * @author lianjiao (http://www.lianjiao.net)
 * @version 1.0
 * @created 2013-4-12
 */
public class UIHelper {
	public static boolean statu = false;
	
	/**
	 * 弹出Toast消息
	 * @param msg
	 */
	public static void ToastMessage(Context cont,String msg)
	{
		Toast.makeText(cont, msg, Toast.LENGTH_SHORT).show();
	}
	public static void ToastMessage(Context cont,int msg)
	{
		Toast.makeText(cont, msg, Toast.LENGTH_SHORT).show();
	}
	public static void ToastMessage(Context cont,String msg,int time)
	{
		Toast.makeText(cont, msg, time*1000).show();
	}
	public static void ToastMessage(Context cont,int msg,int time)
	{
		Toast.makeText(cont, cont.getString(msg), time*1000).show();
	}
	public static void show(Context cont,String msg){
		ToastMessage(cont,msg);
	}
	/**
	 * 发送App异常崩溃报告
	 * @param cont
	 * @param crashReport
	 */
	public static void sendAppCrashReport(final Context cont, final String crashReport)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(cont);
		builder.setIcon(android.R.drawable.ic_dialog_info);
		builder.setTitle(R.string.app_error);
		builder.setMessage(R.string.app_error_message);
		builder.setPositiveButton(R.string.submit_report, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				//发送异常报告
				
//				Uri uri = Uri.parse("mailto:752904147@qq.com");
//				Intent it = new Intent(Intent.ACTION_SENDTO, uri);
//				cont.startActivity(it);
				
				
				Intent i = new Intent(Intent.ACTION_SEND);
				i.setType("text/plain"); //模拟器
				//i.setType("message/rfc822") ; //真机
				i.putExtra(Intent.EXTRA_EMAIL, new String[]{"752904147@qq.com"});
				i.putExtra(Intent.EXTRA_SUBJECT,"lianjiaoAPP - Android客户端 - 错误报告");
				i.putExtra(Intent.EXTRA_TEXT,crashReport);
				cont.startActivity(Intent.createChooser(i, "发送错误报告"));
				//退出
				AppManager.getAppManager().AppExit(cont);
			}
		});
		builder.setNegativeButton(R.string.sure, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				//退出
				AppManager.getAppManager().AppExit(cont);
			}
		});
		builder.show();
	}
	
	/**
	 * 退出程序
	 * @param cont
	 */
	public static void Exit(final Context cont)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(cont);
		builder.setIcon(android.R.drawable.ic_dialog_info);
		builder.setTitle(R.string.app_menu_surelogout);
		builder.setPositiveButton(R.string.sure, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				//退出
				AppManager.getAppManager().AppExit(cont);
			}
		});
		builder.setNegativeButton(R.string.cancle, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.show();
	}	
	
	/**
	 * 消息通知
	 * @param pos 第几个按钮要显示（从0开始）
	 * @param type 显示还是隐藏图标 true显示、false隐藏
	 */
	public static void NewNotice(int pos,boolean type){
		if(type){
			AppContext.NewNotice.setVisibility(View.VISIBLE);
			int screenW = AppContext.DM.widthPixels;// 获取分辨率宽度
	    	int bmpW = screenW/AppContext.FootBtnCount;
	    	bmpW = (int) (bmpW*pos + bmpW*0.6);
	    	RelativeLayout.LayoutParams layoutParam = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
	    	layoutParam.setMargins(bmpW, 0, 0, 0);
	    	AppContext.NewNotice.setLayoutParams(layoutParam);
		}else{
			AppContext.NewNotice.setVisibility(View.INVISIBLE);
			return;
		}
	}
	
	/**
	 * 检测软件更新
	 * @param context
	 * @param isShowDialog
	 * @return
	 */
	public static boolean UpdateSoft(Context context,boolean isShowDialog){
		boolean isUp = false;
//		   UpdateManager manager = new UpdateManager(context);
//			// 检查软件更新
//			try {
//				isUp = manager.checkUpdate(isShowDialog);
//			} catch (NotFoundException e) {
//				e.printStackTrace();
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
		return isUp;
	}
	
	/**
	 * 或者隐藏头部进度条
	 * @param type
	 */
	public static void ShowHeadPro(boolean type){
		AppContext appContext = AppContext.getInstance();
		ProgressBar pro = (ProgressBar)appContext.getMemCache(AppContext.HeaderProBar);
		if(type){
			pro.setVisibility(View.VISIBLE);
		}else{
			pro.setVisibility(View.INVISIBLE);
		}
		
	}
	
	

}
